﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ServiceModel;
using System.Net;

using RilattackLibrary;

namespace RilattackServer
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool roomCreated = false;
        GameConfiguration configuration = new GameConfiguration();

        public MainWindow()
        {
            InitializeComponent();
            App.MainView = this;
            App.PlayerCallBackMap = App.PlayerCallBackMap ?? new Dictionary<ICallBack, Player>();
            playersView.ItemsSource = App.PlayerCallBackMap.Values;
        }

        private void roomCreate_Click(object sender, RoutedEventArgs e)
        {
            if (!roomCreated)
            {
                string endpoint = "net.tcp://";
                try
                {
                    endpoint += System.Configuration.ConfigurationManager.AppSettings["IP"];
                    endpoint += ":" + System.Configuration.ConfigurationManager.AppSettings["Port"];
                    endpoint += "/RilAttack";

                    ServiceHost duplexConnectService;
                    duplexConnectService = new ServiceHost(typeof(Server));
                    duplexConnectService.AddServiceEndpoint(typeof(IServer), new NetTcpBinding(SecurityMode.None), endpoint);
                    duplexConnectService.Open();
                    endPointLbl.Text = "Server is Opened on: " + endpoint;
                    roomCreate.Content = "Lancer la partie";
                    roomCreated = true;
                }
                catch (Exception exc)
                {
                    endPointLbl.Text = "Open Server on \'" + endpoint + "\' Failed : " + exc.ToString();
                    Console.WriteLine("Open Server on \'" + endpoint + "\' Failed : " + exc.ToString());
                }
            }
            else
            {
                RunParty();
            }
        }

        private void RunParty()
        {
            configuration.InitialHP = Convert.ToInt32(initialHP.Value);
            configuration.MaxKills = Convert.ToInt32(maxKills.Value);
            configuration.MaxTime = Convert.ToInt32(maxTime.Value);
            configuration.IsModeTime = modeTime.IsChecked.Value;

            Random rnd = new Random();

            foreach (Player p in App.PlayerCallBackMap.Values)
            {
                p.Life = configuration.InitialHP;
                p.SetRect(8*16, 8*16, Player.playerW, Player.playerH);
            }

            foreach (KeyValuePair<ICallBack, Player> entry in App.PlayerCallBackMap)
            {
                entry.Key.beginNewGame(configuration);
            }

            App.DisplayInGameView();
			App.ServerInstance.GameStarted = true;
        }
    }
}
